﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contracts;
using System.ServiceModel;
using WcfContrib.Errors;
using WcfContrib.Extensions.Context;
using WcfContrib.Handlers;
using WcfContrib.Extensions;

namespace WcfContrib.Showcase.Service
{
    [ServiceBehavior(ConfigurationName = "MyService")]
    ////////////////////////////////
    //NEW FROM 2.0:
    //The extension at its default adds mocking and tracing abilities to the service to be triggered from the client usage
    //This can be set through configuration too.
    //The tracing log file folder location can be customized in the configuration - see the App.config
    ////////////////////////////////
    ////////////////////////////////
    //NEW FROM 2.1.0.0:
    //The log folder location can be set through the new configuration section
    //The ActivationContextExtension is added by default if you're using the ServiceHost that comes with WcfContrib
    //However, if you're not using it and would still like to use the extension - you can do it declarative or in configuration.
    //(ActivationContextInitializerAttribute / ActivationContextExtensionBehaviorElement)
    [ServiceExtensionsSelector]
    class Service : IService
    {
        #region IService Members

        public void Do(Foo foo)
        {
            ShowContextExtension();
            ShowIdentityHelper();

            Console.WriteLine("Service: Do()");
        }

        public void Do2(Foo foo)
        {
            Helpers.WriteLine(ConsoleColor.White, "Service: Do2()");
        }

        void ShowContextExtension()
        {
            Helpers.WriteLine(ConsoleColor.Cyan, "I can get the activation context extension and access all sort of information");
            ActivationContextExtension contextExtension = ActivationContextExtension.Current;
            Helpers.WriteLine(ConsoleColor.Yellow, "Action: {0} | Identity: {1} | Params Count: {2}", contextExtension.Action, contextExtension.IdentityName, contextExtension.Parameters.Length);

            Helpers.WriteLine(ConsoleColor.Cyan, "We can attach items to the context and retreive it whereever we wish");
            if (!contextExtension.CallItems.ContainsKey("1"))
            {
                Helpers.WriteLine(ConsoleColor.Yellow, "Call Items hold items attached to the specific call, key '1' is added");
            }
            if (!contextExtension.SessionItems.ContainsKey("2"))
            {
                Helpers.WriteLine(ConsoleColor.Yellow, "Session Items hold items attached to the entire session, key '2' is added");
                contextExtension.SessionItems.Add("2", "3");
            }
            else
            {
                Helpers.WriteLine(ConsoleColor.Yellow, "Session Items hold items attached to the entire session, key '2' exists already");
            }
        }

        void ShowIdentityHelper()
        {
            Helpers.WriteLine(ConsoleColor.Cyan, "I can get the identity helper to get the client identity from various sources");
            string identityClaimName = IdentityHelper.GetUsername(IdentityScope.ClaimSet);
            Helpers.WriteLine(ConsoleColor.Yellow, "Identity: {0}", identityClaimName);
        }

        #endregion
    }
}
